/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controleurs;

import Entites.Categorie;
import Entites.Materiaux;
import beans.beanGestionCatalogue;
import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author CDI102
 */
public class CategorieMateriaux implements InterfaceControleur {

    private String listCat(HttpServletRequest request) {
        beanGestionCatalogue newCatalog = new beanGestionCatalogue();
        Collection<Categorie> listCat = newCatalog.listerCategorie();
        request.setAttribute("listCat", listCat);
        return "/WEB-INF/Categories/listCategorie.jsp";
    }

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) {

        // SOUS SECTION CATEGORIE

        if ("addCat".equals(request.getParameter("action")) && request.getParameter("cat") != null) {
            System.out.println("---Entrée dans addCat");
            String cat = request.getParameter("cat");
            beanGestionCatalogue newCatalog = new beanGestionCatalogue();
            String parent = request.getParameter("parent");
            Categorie newCategorie = new Categorie();
            newCategorie.setNom(cat);
            newCategorie.setParent(Long.valueOf(parent));
            newCatalog.creerCategorie(newCategorie);
            return listCat(request);
        }

        if ("listCat".equals(request.getParameter("action"))) {
            beanGestionCatalogue newCatalog = new beanGestionCatalogue();
            Collection<Categorie> listCat = newCatalog.listerCategorie();
            request.setAttribute("listCat", listCat);
            return "/WEB-INF/Categories/listCategorie.jsp";
        }

        if ("removeCat".equals(request.getParameter("action")) && request.getParameter("cat") != null) {
            beanGestionCatalogue newCatalog = new beanGestionCatalogue();
            Categorie cat = newCatalog.chercherCategorie(Long.valueOf(request.getParameter("cat")));
            newCatalog.effacerCategorie(cat.getId());
            return "ControleurPrincipal?section=categorieMateriaux.cmd&action=listCat";
        }

        if ("modifCat".equals(request.getParameter("action")) && request.getParameter("cat") != null) {
            System.out.println("----- Entrée dans modifCat");
            beanGestionCatalogue newCatalog = new beanGestionCatalogue();
            Categorie cat = newCatalog.chercherCategorie(Long.valueOf(request.getParameter("cat")));
            Long parent = Long.valueOf(request.getParameter("parent"));
            if (newCatalog.chercherCategorie(parent) != null) {
                cat.setParent(parent);
            }
            cat.setNom(request.getParameter("nomCat"));
            newCatalog.modifierCategorie(cat);
            return "ControleurPrincipal?section=categorieMateriaux.cmd&action=listCat";
        }


        // SOUS SECTION MATERIAUX

        if ("addMat".equals(request.getParameter("action"))) {
            String mat = request.getParameter("mat");
            beanGestionCatalogue newCatalog = new beanGestionCatalogue();
            Collection<Categorie> listCat = newCatalog.listerCategorie();
            request.setAttribute("listCat", listCat);
            if (mat != null) {
                Materiaux newMateriaux = new Materiaux(mat);
                newCatalog.creerMateriaux(newMateriaux);

                return "ControleurPrincipal?section=categorieMateriaux.cmd&action=listMat";
            }
            return "ControleurPrincipal?section=categorieMateriaux.cmd&action=listMat";
        }

        if ("listMat".equals(request.getParameter("action"))) {
            beanGestionCatalogue newCatalog = new beanGestionCatalogue();
            Collection<Materiaux> listMat = newCatalog.listerMateriaux();
            request.setAttribute("listMat", listMat);
            return "/WEB-INF/Materiaux/listMateriaux.jsp";
        }
        if ("removeMat".equals(request.getParameter("action")) && request.getParameter("mat") != null) {
            beanGestionCatalogue newCatalog = new beanGestionCatalogue();
            Materiaux mat = newCatalog.chercherMateriaux(Long.valueOf(request.getParameter("mat")));
            newCatalog.effacerMateriaux(mat.getId());
            return "ControleurPrincipal?section=categorieMateriaux.cmd&action=listMat";
        }
        if ("modifMat".equals(request.getParameter("action")) && request.getParameter("mat") != null && request.getParameter("nomMat") != null) {
            beanGestionCatalogue newCatalog = new beanGestionCatalogue();
            Materiaux mat = newCatalog.chercherMateriaux(Long.valueOf(request.getParameter("mat")));
            mat.setNom(request.getParameter("nomMat"));
            newCatalog.modifierMateriaux(mat);
            return "ControleurPrincipal?section=categorieMateriaux.cmd&action=listMat";
        }
        return "/WEB-INF/accueil.jsp";
    }
}
